<Style>
    .import {
        opacity: 0;
        position: absolute;
        width: 69px;
        height: 38px;
        margin-left: -10px;
    }

</Style>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">货位编码：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="code" class="layui-input" type="text"
                                               placeholder="请输入货位编码"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">仓库：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="queryWarehouseId" name="queryWarehouseId" lay-filter="queryWarehouseId" lay-search
                                                lay-verify=""></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">库区：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="queryWarehouseAreaId" name="queryWarehouseAreaId" lay-filter="queryWarehouseAreaId"
                                                lay-search lay-verify=""></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">货架：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="shelfId" name="shelfId" lay-search lay-verify=""></select>
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label">状态：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="status" name="status" lay-search lay-verify="">
                                            <option value="">请选择状态</option>
                                            <option value="1">启用</option>
                                            <option value="0">停用</option>
                                            <option value="2">作废</option>
                                        </select>
                                    </div>
                                </div>

                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog"
                                            lay-submit>查询
                                    </button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="warehouseShelfFlatBtnAdd" class="layui-btn icon-btn color-reseda">添加平仓库位</button>
                            <button id="warehouseShelfBtnAdd" class="layui-btn icon-btn color-reseda">添加立体库位</button>
                            <button id="syncShelfSpace" class="layui-btn icon-btn color-reseda">拉取畅捷通库位</button>
                            <button id="syncShelfSpaceTemplate" class="layui-btn icon-btn color-reseda">下载模板</button>
                            <button id="productDetailBtnImport" class="layui-btn icon-btn color-reseda">
                                <input id="uploaderInput" type="file" name="file" class="import" accept=".xls,.xlsx" />
                                导入
                            </button>

                        <!--    <button id="importSyncShelfSpace" class="layui-btn icon-btn color-reseda">导入库位</button>-->
                        </div>
                        <div class="lay-btn-rt">

                        </div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="warehouseShelfSpaceTable" lay-filter="warehouseShelfSpaceTable"></table>
        </div>
    </div>
</div>
<script type="text/html" id="warehouseShelfFlatForm">
    <form lay-filter="warehouseShelfFlatForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select id="warehouseFlatId" name="warehouseId" lay-filter="warehouseFlatId" lay-search lay-verify="required"></select>
                </div>
            </div>

            <div class="layui-col-xs6">
                <label class="layui-form-label">库区</label>
                <div class="layui-input-block">
                    <select id="warehouseAreaFlatId" name="warehouseAreaId" lay-search lay-verify="required"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">初始编码</label>
                <div class="layui-input-block">
                    <input name="firstCode" placeholder="请输入初始编码" type="text" class="layui-input"
                           lay-verify="required"
                           required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">库位数</label>
                <div class="layui-input-block">
                    <input name="spaceNum" placeholder="请输入库位数" type="text" class="layui-input"
                           lay-verify="required|isOnlySn"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status">
                        <option value="enable">启用</option>
                        <option value="unable">停用</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input"
                           lay-verify="sortNumber"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="description" type="text" class="layui-input" style="height: 100px;"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseShelfFlatFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 表单弹窗 -->
<script type="text/html" id="warehouseShelfForm">
    <form lay-filter="warehouseShelfForm" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">名称</label>
                <div class="layui-input-block">
                    <input name="name" placeholder="请输入名称" type="text" class="layui-input"
                           lay-verify="required|isOnlyName"
                           required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">编码</label>
                <div class="layui-input-block">
                    <input name="code" placeholder="请输入编码" type="text" class="layui-input"
                           lay-verify="required|isOnlySn"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">仓库</label>
                <div class="layui-input-block">
                    <select id="warehouseId" name="warehouseId" lay-filter="warehouseId" lay-search lay-verify="required"></select>
                </div>
            </div>

            <div class="layui-col-xs6">
                <label class="layui-form-label">库区</label>
                <div class="layui-input-block">
                    <select id="warehouseAreaId" name="warehouseAreaId" lay-search lay-verify="required"></select>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">货架列数</label>
                <div class="layui-input-block">
                    <input name="columnNum" placeholder="请输入货架列数" type="text" class="layui-input"
                           lay-verify="required|sortNumber" required/>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">货架层数</label>
                <div class="layui-input-block">
                    <input name="layerNum" placeholder="请输入货架层数" type="text" class="layui-input"
                           lay-verify="required|sortNumber" required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">货架图片</label>
            <div class="layui-upload-drag cardU" id="pictureDiv">
                <!--<i class="layui-icon"></i>
                <p>点击上传，或将文件拖拽到此处</p>-->
                <input type="hidden" name="picture" lay-verify="" data-name="仓库图片">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-col-xs6">
                <label class="layui-form-label">状态</label>
                <div class="layui-input-block">
                    <select name="status">
                        <option value="enable">启用</option>
                        <option value="unable">停用</option>
                    </select>
                </div>
            </div>
            <div class="layui-col-xs6">
                <label class="layui-form-label">排序</label>
                <div class="layui-input-block">
                    <input name="sortNumber" placeholder="请输入排序" type="text" class="layui-input"
                           lay-verify="sortNumber"
                           required/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注描述</label>
            <div class="layui-input-block">
                <textarea name="description" type="text" class="layui-input" style="height: 100px;"/>
            </div>
        </div>

        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="warehouseShelfFormSubmit" lay-submit>保存</button>
        </div>
    </form>
</script>
<!-- 表格操作列 -->
<script type="text/html" id="warehouseShelfSpaceTableBar">
    <a class="layui-btn layui-btn-xs" lay-event="printBarcode">打印条码</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">作废</a>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'element'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;
        var element = layui.element;


        //仓库列表
        admin.initDataSelect("enterprisecab/warehouse/admin/warehouse/list", {limit: 9999}, "queryWarehouseId", null, "仓库", null, null);
        //仓库选择事件
        form.on('select(queryWarehouseId)', function (data) {
            $("#queryWarehouseAreaId").empty();
            //仓库库区列表
            admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {
                limit: 9999,
                warehouseId: data.value
            }, "queryWarehouseAreaId", null, "库区", null, null);
        });
        //仓库库区选择事件
        form.on('select(queryWarehouseAreaId)', function (data) {
            $("#shelfId").empty();
            //库区货架列表
            admin.initDataSelect("enterprisecab/warehouse/admin/warehouseShelf/list", {
                limit: 9999,
                warehouseAreaId: data.value
            }, "shelfId", null, "库区货架", null, null);
        });

        // 渲染表格
        var insTb = table.render({
            elem: '#warehouseShelfSpaceTable',
            url: config.base + 'enterprisecab/warehouse/admin/warehouseShelfSpace/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 80}
                , {field: 'code', align: 'center', title: '货位编码'}
                , {field: 'warehouseName', align: 'center', title: '仓库'}
                , {field: 'warehouseAreaName', align: 'center', title: '库区'}
                , {field: 'shelfName', align: 'center', title: '货架'}
                , {field: 'statusDesc', align: 'center', title: '状态'}
                , {field: 'description', align: 'center', title: '备注'}
                , {field: 'createTime', align: 'center', title: '创建时间', width: 180}
                /*, {align: 'center', toolbar: '#warehouseShelfSpaceTableBar', title: '操作', width: 200}*/
                , {
                    align: 'center',
                    title: '操作',
                    width: 200,
                    templet: function(d){
                        // 根据 status 的值决定是否展示作废按钮
                        let cancelBtn = d.status != 'cancel' ? '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="cancel">作废</a>' : '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="recover">恢复</a>';


                        return '<a class="layui-btn layui-btn-xs" lay-event="printBarcode">打印条码</a>' + cancelBtn;
                    }
                }
            ]]
        });

        // 工具条点击事件
        table.on('tool(warehouseShelfSpaceTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'printBarcode') { // 打印
                admin.getPrintBarcode('enterprisecab/warehouse/admin/palletBoxList/getBarCode', data.code);
            }else if(obj.event === 'cancel'){
                cancelShelfSpace(obj);
            }else if(obj.event === 'recover'){
                recoverShelfSpace(obj);
            }
        });

        //监听排序
        table.on('sort(warehouseShelfSpaceTable)', function (obj) {
            table.reload('warehouseShelfSpaceTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });


        // 模板下载
        $('#syncShelfSpaceTemplate').click(function () {
            window.location.href = config.base + 'enterprisecab/warehouse/admin/warehouseShelfSpace/template';
        });


        $('#syncShelfSpace').click(function () {
            syncChangJieTongFunction();
        });



        function syncChangJieTongFunction() {
            layer.confirm('确定要同步货位数据吗？', {
                offset: '265px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprisecab/warehouse/admin/warehouseShelfSpace/syncSpaceCjt', {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg("操作成功", {icon: 1});
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                    table.reload('warehouseShelfSpaceTable');
                    layer.closeAll('page');
                }, 'GET');
            });
        }


        //导入excel
        $('#uploaderInput').on('change', function (e) {
            layer.load(2);
            var formData = new FormData();
            var files = e.target.files;
            let that = $(this);
            formData.append('file', files[0]);
            formData.append('name', files[0].name); //这个地方可以传递多个参数
            $.ajax({
                url: config.base + 'enterprisecab/warehouse/admin/warehouseShelfSpace/importExcel',
                data: formData,
                headers: { Authorization: 'Bearer ' + config.getToken() },
                type: 'POST',
                processData: false,
                contentType: false,
                success: function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, { icon: 1 });
                        layer.closeAll('page');
                        setTimeout((_) => {
                            location.reload();
                        }, 1000);
                    } else {
                        layer.msg(res.msg, { icon: 2 });
                    }
                    that.val('');
                },
            });
        });

        /**
         * 作废货位
         */
        function cancelShelfSpace(obj) {
            let url = 'enterprisecab/warehouse/admin/warehouseShelfSpace/cancel/'+obj.data.id
            layer.confirm('确定要作废库位吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req(url, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('warehouseShelfSpaceTable');
                        // obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'GET');
            });
        }

        function recoverShelfSpace(obj) {
            let url = 'enterprisecab/warehouse/admin/warehouseShelfSpace/recover/'+obj.data.id
            layer.confirm('确定要恢复库位吗？', {
                offset: '65px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req(url, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        table.reload('warehouseShelfSpaceTable');
                        // obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'GET');
            });
        }




        // 添加按钮点击事件
        $('#warehouseShelfFlatBtnAdd').click(function () {
            showFlatEditModel();
        });


        function showFlatEditModel(data) {
            admin.open({
                type: 1,
                area: '860px',
                offset: ['120px', '550px'],
                title: data ? '修改仓库货架' : '添加仓库货架',
                content: $('#warehouseShelfFlatForm').html(),
                success: function () {
                    //仓库列表
                    admin.initDataSelect("enterprisecab/warehouse/admin/warehouse/list", {limit: 9999}, "warehouseFlatId", data ? data.warehouseId : null, "仓库", null, null);
                    //仓库选择事件
                    form.on('select(warehouseFlatId)', function (data) {
                        $("#warehouseAreaFlatId").empty();
                        //仓库库区列表
                        admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.value}, "warehouseAreaFlatId", null, "库区", null, null);
                    });
                    // 表单提交事件
                    form.on('submit(warehouseShelfFlatFormSubmit)', function (d) {
                        layer.load(2);
                        var url = 'enterprisecab/warehouse/admin/warehouseShelfSpace/addFlat';
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseShelfSpaceTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

        // 添加按钮点击事件
        $('#warehouseShelfBtnAdd').click(function () {
            showEditModel();
        });

        // 显示编辑弹窗
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: '860px',
                offset: '120px',
                title: data ? '修改仓库货架' : '添加仓库货架',
                content: $('#warehouseShelfForm').html(),
                success: function () {
                    debugger;
                    //仓库列表
                    admin.initDataSelect("enterprisecab/warehouse/admin/warehouse/list", {limit: 9999}, "warehouseId", data ? data.warehouseId : null, "仓库", null, null);
                    //仓库选择事件
                    form.on('select(warehouseId)', function (data) {
                        $("#warehouseAreaId").empty();
                        //仓库库区列表
                        admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.value}, "warehouseAreaId", null, "库区", null, null);
                    });

                    //图片上传
                    upload.render({
                        elem: '#pictureDiv'
                        , url: config.upload_url + 'upload?path=' + img_path
                        , auto: true
                        , acceptMime: 'image/*'
                        , headers: {authorization: 'Bearer ' + config.getToken()}
                        , done: function (res) {
                            layer.closeAll('loading');
                            layer.msg('上传成功');
                            var imgUrl = res.data.url;
                            //上传完毕回调
                            $("#pictureDiv").html('<img src="' + imgUrl + '" alt="上传成功后渲染"><input type="hidden" name="picture" value="' + imgUrl + '">');
                        }
                        , error: function (data) {
                            layer.closeAll('loading');
                        }
                    });

                    if (data != undefined) {
                        var picture = data.picture;
                        if (picture) {
                            $("#pictureDiv").html('<img src="' + picture + '" alt="上传成功后渲染" style="max-width: 196px"><input type="hidden" name="picture" value="' + picture + '">');
                        }
                        //仓库库区列表
                        admin.initDataSelect("enterprisecab/warehouse/admin/warehouseArea/list", {limit: 9999,warehouseId: data.warehouseId}, "warehouseAreaId", data.warehouseAreaId, "库区", null, null);
                        //数据回显
                        form.val('warehouseShelfForm', data);
                    }

                    if (data) {
                        var option = $("form select[name='status']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.status == item.text) {
                                data.status = item.value;
                            }
                        });
                    }

                    if (data) {
                        var option = $("form select[name='warehouseFeatureType']").find("option");
                        layui.each(option, function (index, item) {
                            if (data.warehouseFeatureType == item.text) {
                                data.warehouseFeatureType = item.value;
                            }
                        });
                    }

                    form.val('warehouseShelfForm', data);
                    // 表单提交事件
                    form.on('submit(warehouseShelfFormSubmit)', function (d) {
                        debugger
                        layer.load(2);
                        var url = 'enterprisecab/warehouse/admin/warehouseShelf/add';
                        if (data) {
                            url = 'enterprisecab/warehouse/admin/warehouseShelf/update';
                        }
                        admin.req(url, d.field, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('warehouseShelfSpaceTable');
                                layer.closeAll('page');
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, data ? 'PUT' : 'POST');
                        return false;
                    });
                }
            });
        }

    });
</script>